package com.example.pandaso.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.pandaso.mapper.UserMapper;
import com.example.pandaso.pojo.ChatOutputRequest;
import com.example.pandaso.pojo.User;
import com.example.pandaso.pojo.UserEngine;
import com.example.pandaso.service.UserInface;
import com.fasterxml.jackson.databind.ObjectMapper;

@Service
public class UserService implements UserInface {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean isValidUser(String username, String password) {
        User user = userMapper.findByUsername(username);
        if (user == null) {
            return false;
        }
        return password.equals(user.getUserpassword());

    }

    @Override
    public Map<String, Boolean> findEnginesByUserId(int userId) {
        List<UserEngine> userEngines = userMapper.findEnginesByUserId(userId);
        Map<String, Boolean> _userengine = new HashMap();
        for (UserEngine e : userEngines) {
            _userengine.put(e.getEngineName(), e.getEnabled() == 1);
        }
        return _userengine;
    }

    @Override
    public int findUserIdByuserName(String userName) {
        return userMapper.findUserIdByuserName(userName);
    }

    @Override
    public User findUserInfoByUserName(String userName) {
        return userMapper.findByUsername(userName);
    }

    @Override
    public int updateUserEngines(Map<String, Boolean> userEngines, int userId) {
        int count = 0;
        List<UserEngine> userEngines_0 = userMapper.findEnginesByUserId(userId);
        for (UserEngine e : userEngines_0) {
            Boolean value = userEngines.get(e.getEngineName());
            // 如果数据值不同，就更新
            if (value != (e.getEnabled() == 1)) {
                e.setEnabled(value == true ? 1 : 0);
                userMapper.updateUserEngines(e.getEnabled(), userId, e.getEngineName());
                count++;
            }
        }
        return count;
    }

    public Boolean isValiName(String username) {
        User user = userMapper.findByUsername(username);
        if (user == null) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean isValiEmail(String useremail) {
        User user = userMapper.findByUseremail(useremail);
        if (user == null) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean isEnroll(User user) {
        Boolean myuser = userMapper.addByUser(user);
        if (myuser) {
            return true;
        }
        return false;
    }

    @Override
    public int updateUserChatoutPut(String chatOutput, int userId) {
        return userMapper.updateUserChatoutPut(chatOutput, userId);
    }

    @Override
    public ChatOutputRequest getUserChatOutput(int userId) {

        String json = userMapper.getChatOutputByUserId(userId);
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper.readValue(json, ChatOutputRequest.class);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public int updateUserPassword(String newPassword, int userId) {
        int isupdate = userMapper.updateUserPasswordByUserId(newPassword, userId);
        return isupdate;
    }

    @Override
    public Boolean findLoginTime(int userId, String loginTime) {
        Boolean isupdate = userMapper.addLoginTime(userId, loginTime);
        return isupdate;
    }

    @Override
    public List<String> findLoginHistory(int userId) {
        List<String> listHistory = userMapper.updateLoginHistory(userId);
        return listHistory;
    }

    @Override
    public Boolean findSearch(int userId, String search) {
        Boolean isupdate = userMapper.addSearch(userId, search);
        return isupdate;
    }

    @Override
    public List<String> findsaveSearch(int userId) {
        List<String> listSearch = userMapper.updateUserSearch(userId);
        return listSearch;
    }

    @Override
    public Boolean deleteSearch(int userId) {
        Boolean listSearch = userMapper.deleteUserSeach(userId);
        return listSearch;
    }

}